<!-- 排程一览表 -->
<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :defaults.sync="defaults"
      :data="data"
      :page.sync="page"
      :search.sync="search"
      :permission="permissionList"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
      @select="handSelect"
      @select-all="selectionAll"
    >
      <template slot="menu" slot-scope="{ row }">
        <el-button type="text" size="small" icon="el-icon-view" @click.stop="handleView(row)">详情</el-button>
      </template>

      <template slot="menuLeft">
        <el-button type="warning" size="small" icon="el-icon-download" @click="$refs.exportComp.visible = true">导出</el-button>
      </template>
    </avue-crud>

    <!-- 计划排程 -->
    <el-dialog
      title="计划排程"
      v-if="dialogVisible"
      custom-class="scheduleListDialog"
      append-to-body
      :fullscreen="true"
      :close-on-click-modal="false"
      :visible.sync="dialogVisible"
      :before-close="handleCloseDispatchDialog"
    >
      <div v-loading="dispatchDialog.loading">
        <div class="dispatch-title">
          <div class="dispatch-title-dot"></div>
          <div class="dispatch-title-text">基本信息</div>
        </div>
        <avue-form ref="workInfoForm" class="workInfoForm" v-model="dispatchDialog.workOrderInfo.form" :option="dispatchDialog.workOrderInfo.options"></avue-form>
        <div class="dispatch-title">
          <div class="dispatch-title-dot"></div>
          <div class="dispatch-title-text">产线派工详情</div>
        </div>
        <avue-crud ref="dispatchCrud" :table-loading="dispatchDialog.plan.loading" :option="dispatchDialog.plan.option" :data="dispatchDialog.plan.data"> </avue-crud>
        <div class="dispatch-title">
          <div class="dispatch-title-dot"></div>
          <div class="dispatch-title-text">生产进展详情</div>
        </div>
        <avue-crud ref="dispatchCrud" :table-loading="dispatchDialog.progress.loading" :option="dispatchDialog.progress.option" :data="dispatchDialog.progress.data">
        </avue-crud>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" icon="el-icon-circle-close" @click="handleCloseDispatchDialog">关闭</el-button>
      </span>
    </el-dialog>
    <export-comp
      ref="exportComp"
      page-api-url="mes-product/workorderPlan/page-workOrder-with-plan"
      reportCode="workorderPlan_export"
      :option="option"
      :page="page"
      :query="query"
      file-name="排程一览表"
    >
    </export-comp>
  </basic-container>
</template>

<script>
import { getList, getProgressDetail, getDetail } from '@/api/product/scheduleList';
import { mapGetters, mapState } from 'vuex';
import exportComp from '@/components/export-comp';

export default {
  name: 'workorderPlan',
  components: {
    exportComp
  },
  data() {
    return {
      dialogVisible: false,
      form: {},
      query: {},
      search: {},
      defaults: {},
      loading: true,
      data: [],
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'calc(100vh - 360px)',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchSpan: 4,
        searchMenuSpan: 4,
        searchMenuPosition: 'right',
        searchLabelWidth: 80,
        border: true,
        index: true,
        indexLabel: '序号',
        viewBtn: false,
        editBtn: false,
        addBtn: false,
        delBtn: false,
        selection: true,
        dialogClickModal: false,
        menu: true,
        menuWidth: 100,
        labelWidth: 160,
        dialogDrag: true,
        highlightCurrentRow: true,
        columnBtn: false,
        column: [
          {
            label: '工单号',
            prop: 'workOrderNo',
            width: 150,
            searchOrder: 1,
            searchLabelWidth: 70,
            search: true,
            overHidden: true,
            rules: [
              {
                required: true,
                message: '请输入工单号',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '产线',
            prop: 'lineName',
            searchLabelWidth: 60,
            hide: false,
            search: true,
            searchOrder: 2
          },
          {
            label: '产品编码',
            prop: 'productPartNo',
            type: 'search',
            overHidden: true,
            dicData: [],
            placeholder: '请输入 产品编码'
          },
          {
            label: '产品型号',
            prop: 'productPartModel',
            search: false,
            overHidden: true,
            searchOrder: 6,
            rules: [
              {
                required: true,
                message: '请输入产品名称',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '客户编码',
            prop: 'customerCode',
            search: true,
            searchOrder: 4
          },
          {
            label: '客户名称',
            prop: 'customerName'
          },
          {
            label: '要求发货时间',
            width: 150,
            prop: 'requiredDeliveryDate'
          },

          {
            label: '计划结束时间',
            prop: 'planEndTime',
            type: 'datetime',
            width: 150,
            format: 'yyyy-MM-dd HH:mm:ss',
            valueFormat: 'yyyy-MM-dd HH:mm:ss'
          },
          {
            label: '工单状态',
            prop: 'status',
            type: 'select',
            width: 80,
            html: true,
            formatter: (val) => this.$statusText.GD(val.status),
            props: {
              label: 'dictValue',
              value: 'dictKey'
            },
            dicUrl: '/api/blade-system/dict/dictionary?code=mfg_work_order',
            rules: [
              {
                required: true,
                message: '请输入状态',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '下单时间',
            prop: 'createTime',
            type: 'datetime',
            width: 150,
            format: 'yyyy-MM-dd HH:mm:ss',
            valueFormat: 'yyyy-MM-dd HH:mm:ss'
          },
          {
            label: '排程时间',
            prop: 'pcTime',
            type: 'datetimerange',
            hide: true,
            searchOrder: 3,
            searchSpan: 6,
            search: true,
            searchType: 'datetimerange',
            format: 'yyyy-MM-dd HH:mm:ss',
            valueFormat: 'yyyy-MM-dd HH:mm:ss',
            startPlaceholder: '计划开始时间',
            endPlaceholder: '计划完成时间'
          },
          {
            label: '排程时间',
            prop: 'planTime',
            width: 150
          },
          {
            label: '排程人员',
            prop: 'planUserName',
            width: 100
          }
        ]
      },
      dispatchDialog: {
        loading: false,
        workOrderInfo: {
          form: {},
          options: {
            submitBtn: false,
            emptyBtn: false,
            labelWidth: 80,
            detail: false,
            column: [
              {
                label: '工单号',
                prop: 'workOrderNo',
                disabled: true,
                span: 6
              },
              {
                label: '产品编码',
                prop: 'productPartNo',
                disabled: true,
                span: 6
              },
              {
                label: '产品名称',
                prop: 'productPartName',
                disabled: true,
                span: 6
              },
              {
                label: '产品型号',
                prop: 'productPartModel',
                disabled: true,
                span: 6
              },
              {
                label: '工单数量',
                prop: 'planQuantity',
                disabled: true,
                span: 6
              },
              {
                label: '工单状态',
                prop: 'status',
                type: 'select',
                disabled: true,
                span: 6,
                props: {
                  label: 'dictValue',
                  value: 'dictKey'
                },
                dicUrl: '/api/blade-system/dict/dictionary?code=mfg_work_order'
              },
              {
                label: '客户编码',
                prop: 'customerCode',
                disabled: true,
                span: 6
              },
              {
                label: '客户名称',
                prop: 'customerName',
                disabled: true,
                span: 6
              }
            ]
          }
        },
        plan: {
          loading: false,
          option: {
            height: '25vh',
            calcHeight: 30,
            tip: false,
            searchShow: true,
            searchMenuSpan: 5,
            searchLabelWidth: 100,
            border: true,
            index: true,
            indexLabel: '序号',
            viewBtn: false,
            editBtn: false,
            addBtn: false,
            delBtn: true,
            refreshBtn: false,
            columnBtn: false,
            addRowBtn: false,
            cancelBtn: false,
            selection: false,
            dialogClickModal: false,
            menu: false,
            labelWidth: 160,
            dialogDrag: true,
            highlightCurrentRow: true,
            column: [
              {
                label: '日期',
                prop: 'dayPlanDate'
              },
              {
                label: '产线',
                prop: 'lineName'
              },
              {
                label: '工段',
                prop: 'sectionName'
              },
              {
                label: '计划数量',
                prop: 'dayPlanQuantity'
              },
              {
                label: '计划工时',
                prop: 'dayPlanWorkHour'
              }
            ]
          },
          data: []
        },
        progress: {
          loading: false,
          option: {
            height: '25vh',
            calcHeight: 30,
            tip: false,
            searchShow: true,
            searchMenuSpan: 5,
            searchLabelWidth: 100,
            border: true,
            index: true,
            indexLabel: '序号',
            viewBtn: false,
            editBtn: false,
            addBtn: false,
            delBtn: true,
            refreshBtn: false,
            columnBtn: false,
            addRowBtn: false,
            cancelBtn: false,
            selection: false,
            dialogClickModal: false,
            menu: false,
            labelWidth: 160,
            dialogDrag: true,
            highlightCurrentRow: true,
            column: [
              {
                label: '日期',
                prop: 'createTime'
              },
              {
                label: '产线',
                prop: 'lineName'
              },
              {
                label: '工段',
                prop: 'sectionName'
              },
              {
                label: '投入数量',
                prop: 'inputQuantity'
              },
              {
                label: '完工数量',
                prop: 'completedQuantity'
              },
              {
                label: '累计工时',
                prop: 'totalWorkHour'
              },
              {
                label: '不良数量',
                prop: 'ngQuantity'
              },
              {
                label: '在制数量',
                prop: 'wipQuantity'
              }
            ]
          },
          data: []
        }
      }
    };
  },
  computed: {
    ...mapGetters(['permission']),
    ...mapState({
      realName: (state) => state.user.userInfo.real_name,
      userId: (state) => state.user.userInfo.user_id
    }),
    permissionList() {
      return {};
    }
  },
  mounted() {
    this.$nextTick(() => {
      //搜索与表格结构分隔
      this.$structuralSplit();
    });
  },
  methods: {
    async handleView(row) {
      this.dialogVisible = true;
      this.dispatchDialog.loading = true;
      this.dispatchDialog.workOrderInfo.form = structuredClone(row);
      try {
        let [resA, resB] = await Promise.all([getDetail(row.workOrderId), getProgressDetail(row.workOrderId)]);
        let planData = resA.data.data;
        this.dispatchDialog.plan.data = planData;
        let progressData = resB.data.data;
        this.dispatchDialog.progress.data = progressData;
      } catch (error) {
      } finally {
        this.dispatchDialog.loading = false;
      }
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      if (params.datetimerange) {
        params.planStartDate = params.datetimerange[0];
        params.planEndDate = params.datetimerange[1];
        delete params.datetimerange;
      }
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud && this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      console.log(this.query, 'this.query');
      if (this.query.pcTime && this.query.pcTime.length > 0) {
        this.query.planTimeStart = this.query.pcTime[0];
        this.query.planTimeEnd = this.query.pcTime[1];
        delete this.query.pcTime;
      }
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    handSelect() {
      this.$refs.crud.setCurrentRow();
    },
    selectionAll() {
      this.$refs.crud.setCurrentRow();
    },
    handleCloseDispatchDialog() {
      const that = this;
      that.clearDiapatchData();
    },
    clearDiapatchData() {
      const that = this;
      that.dialogVisible = false;
      that.dispatchDialog.plan.data = [];
      that.dispatchDialog.progress.data = [];
      that.$refs.workInfoForm.resetFields();
      that.$refs.crud && that.$refs.crud.toggleSelection();
    },
    // 重新加载公用表格弹窗组件
    newKey() {
      this.timer = new Date().getTime();
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .scheduleListDialog {
  .el-dialog__body {
    padding: 0px 20px 5px;
  }
  .avue-crud__menu {
    min-height: auto;
    margin-bottom: 15px;
  }
  .el-table__fixed-header-wrapper {
    .el-icon-plus {
      cursor: pointer;
      font-weight: bold;
      color: rgb(102, 177, 255);
    }
  }
  // .el-table__body td {
  //   padding: 0;
  // }
  .dispatch-title {
    display: flex;
    align-items: center;
    margin: 30px 0 20px 0;
    &-dot {
      background-color: rgb(102, 177, 255);
      width: 10px;
      height: 20px;
      margin-right: 8px;
    }
    &-text {
      font-weight: bold;
      font-size: 16px;
    }
  }
  .dispatch-menu {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .workInfoForm {
    .avue-form__group {
      .el-form-item {
        margin-bottom: 5px;
      }
      .avue-form__menu {
        display: none;
      }
    }
  }
  .el-dialog__footer {
    padding: 10px 20px 10px;
  }
}
</style>
